# -*- coding: utf-8 -*-
'''eof
name:申请人当前存在五级分类为关注类的贷款
code:IC19_IndCurr5ClassConsCheck
tableName:
columnName:
groups:行内二代个人征信数据
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:申请人当前存在五级分类为关注类的未结清贷款账户数
eof'''


null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
    获取表信息
    :param data:表上层的字典对象
    :param tableName:表的名字
    :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
    :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
    :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
    '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]



def IC19_IndCurr5ClassConsCheck():
    data = IC_DSJ_2D.get('data')
    # 借贷账户信息 ,出现次数[1..1],下级字段出现次数[0..*]
    PDA = getTable(data, 'PDA', 1, 0)
    if PDA == [{}]:
        return u'缺失值'
    elif PDA == 0:
        return 0
    else:
        #借贷账户信息单元 ,出现次数[0..*]，下级字段出现次数[1:1]
        PD01 = getTable(PDA[0], 'PD01', 0, 1)
        if PD01 == [{}]:
            return u'缺失值'
        elif PD01 == 0:
            return 0
        else:
            cnts = 0
            Account_list = []
            for i in PD01:
                try:
                    #基本信息段 ,出现次数[1..1]，下级字段出现次数[1..1]
                    PD01A = getTable(i, 'PD01A', 1, 1)
                    #最新表现信息段 ,出现次数[1..1]，下级字段出现次数[1..1]
                    PD01B = getTable(i, 'PD01B', 1, 1)
                    if PD01A not in [0,[{}]] and PD01B not in [0,[{}]]:
                        #获取账户类型
                        PD01AD01 = PD01A[0].get('PD01AD01')
                        #获取五级分类
                        ##最新表现信息段取一次
                        fiveClassType = PD01B[0].get('PD01BD03')
                        if fiveClassType in null_type_list:
                            #最近一次月度表现信息段 ,出现次数[1..1]，下级字段出现次数[0..1]
                            PD01C = getTable(i, 'PD01C', 1, 0)
                            if PD01C in [0,[{}]]:  #按理来说，PD01C为0是正常的，但这样造成获取不到五级分类，因此判断为异常情况
                                cnts += 1
                                continue
                            elif PD01C[0].get('PD01CD02') in null_type_list: #判断为缺失情况的原因在于，造成无法获取五级分类
                                cnts += 1
                                continue
                            else:
                                fiveClassType = PD01C[0].get('PD01CD02')
                        else:
                            pass
                        #获取账户状态
                        ##最新表现信息段取一次
                        accountType = PD01B[0].get('PD01BD01')
                        #账户编号
                        PD01AI01 = PD01A[0].get('PD01AI01')
                        if accountType in null_type_list:    #如果accountType的字段值属于空集，就在最近一次月度表现信息段中取
                            #最近一次月度表现信息段 ,出现次数[1..1]，下级字段出现次数[0..1]
                            PD01C = getTable(i, 'PD01C', 1, 0)
                            if PD01C in [0,[{}]]:  #按理来说，PD01C为0是正常的，但这样造成获取不到五级分类，因此判断为异常情况
                                cnts += 1
                                continue
                            elif PD01C[0].get('PD01CD01') in null_type_list: #判断为缺失情况的原因在于，造成无法获取五级分类
                                cnts += 1
                                continue
                            else:
                                accountType = PD01C[0].get('PD01CD01')
                        else:
                            pass
                        if PD01AD01  in null_type_list or fiveClassType in null_type_list \
                            or accountType in null_type_list or PD01AI01 in null_type_list:
                            cnts += 1
                        else:
                            if PD01AD01 == u"D1" and fiveClassType in [2,"2"] and accountType not in [3,5,"3","5"]:
                                Account_list.append(PD01AI01)
                            if PD01AD01 in ["R1","R4"] and fiveClassType in [2,"2"] and accountType not in [3,"3"]:
                                Account_list.append(PD01AI01)
                    elif PD01A == 0 and PD01B == 0:
                        pass
                    else:
                        cnts +=1
                except:
                    cnts += 1
            if cnts == len(PD01):
                return u'缺失值'
            Account_list = list(filter(lambda x:x  not in null_type_list,Account_list))
            return len(set(Account_list))
result = IC19_IndCurr5ClassConsCheck()
